package com.ourteam.product.model.api;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractServiceController;
import com.ourteam.frame.web.AbstractSearchHelper;
import com.ourteam.frame.web.GridBean;
import com.ourteam.product.model.dao.ProductModelPackageDAOQueryBean;
import com.ourteam.product.model.domain.ProductModelPackageBean;
import com.ourteam.product.model.service.IProductModelService;
import com.ourteam.product.model.service.ProductModelServiceFactory;

@Controller
@CrossOrigin
@RequestMapping("/api/PRODUCT/MODEL/productModelPackage")
public class ProductModelPackageAPI extends AbstractServiceController {

	private static final IProductModelService PRODUCT_MODEL_SERVICE = ProductModelServiceFactory
			.getProductModelService();

	@RequestMapping("/saveModelPackage")
	@ResponseBody
	public ProductModelPackageBean saveModelPackage(@ModelAttribute ProductModelPackageBean modelPackageBean,
			HttpServletRequest request, Model model) throws Exception {

		if (modelPackageBean.getId() != 0) {
			PRODUCT_MODEL_SERVICE.modifyProductModelPackage(modelPackageBean);
		} else {
			PRODUCT_MODEL_SERVICE.addNewProductModelPackage(modelPackageBean);
		}

		return modelPackageBean;
	}

	@RequestMapping("/getModelPackageById")
	@ResponseBody
	public ProductModelPackageBean getModelPackageById(@RequestParam long id, HttpServletRequest request, Model model)
			throws Exception {

		return PRODUCT_MODEL_SERVICE.getProductModelPackageById(id);

	}

	@RequestMapping("/getModelPackages")
	@ResponseBody
	public ProductModelPackageBean[] getModelPackages(@RequestParam long domainId, @RequestParam String packageType,
			HttpServletRequest request, Model model) throws Exception {

		ProductModelPackageDAOQueryBean queryBean = new ProductModelPackageDAOQueryBean();

		queryBean.createCriteria().andDomainIdEqualTo(domainId).andPackageTypeEqualTo(packageType);

		return PRODUCT_MODEL_SERVICE.queryProductModelPackages(queryBean);

	}

	@RequestMapping("/queryModelPackages")
	@ResponseBody
	public GridBean queryModelPackages(final @ModelAttribute ProductModelPackageSearchModel searchModel,
			HttpServletRequest request, Model model) throws Exception {

		AbstractSearchHelper<ProductModelPackageDAOQueryBean, ProductModelPackageSearchModel> searchHelper = new AbstractSearchHelper<ProductModelPackageDAOQueryBean, ProductModelPackageSearchModel>(
				searchModel) {

			@Override
			protected ProductModelPackageDAOQueryBean createQueryBean() {
				return new ProductModelPackageDAOQueryBean();
			}

			@Override
			protected int queryCount(ProductModelPackageDAOQueryBean queryBean) throws Exception {
				return PRODUCT_MODEL_SERVICE.queryProductModelPackageCount(queryBean);
			}

			@Override
			protected Object[] queryData(ProductModelPackageDAOQueryBean queryBean) throws Exception {
				return PRODUCT_MODEL_SERVICE.queryProductModelPackages(queryBean);
			}

		};

		return searchHelper.doSearch(request);

	}

	@RequestMapping("/deleteModelPackage")
	@ResponseBody
	public boolean deleteModelPackage(@RequestParam long packageId, HttpServletRequest request, Model model)
			throws Exception {

		PRODUCT_MODEL_SERVICE.deleteProductModelPackages(new long[] { packageId });

		return true;

	}

	@RequestMapping("/deleteModelPackages")
	@ResponseBody
	public boolean deleteModelPackages(@RequestParam(name = "ids") long[] packageIds, HttpServletRequest request,
			Model model) throws Exception {

		PRODUCT_MODEL_SERVICE.deleteProductModelPackages(packageIds);

		return true;

	}
	
}
